<?php
/**
 * Created by PhpStorm.
 * User: yyq
 * Date: 2017/12/23
 * Time: 20:22
 */

namespace Information\Controller;


use Common\Controller\AdminbaseController;
use Common\Model\CategoryModel;
use Common\Model\VideoLinkModel;

class VideoLinkController extends AdminbaseController
{
    private $category_model;
    private $video_link_model;

    public function __construct()
    {
        parent::__construct();
        $this->category_model = new CategoryModel();
        $this->video_link_model = new VideoLinkModel();
    }

    public function add() {
        if(IS_POST) {
            $data = I('post.post');
            $data['content'] = htmlspecialchars_decode($data['content']);
            $images = upload_local('video');
            $data['create_time'] = date('Y-m-d H:i:s');
            $data['update_time'] = date('Y-m-d H:i:s');
            if($images) {
                $data['cover'] = $images[0];
            }//dump($data);exit;
            if(!$this->video_link_model->create($data)) {
                $this->error($this->video_link_model->getError());
            }
            if($this->video_link_model->add()) {
                $this->success('添加成功');
            } else {
                $this->error('失败，请重试');
            }
        } else {
            $this->assign('options', $this->category_model->getOption());
            $this->display();
        }

    }

    public function lists() {
        if(IS_POST) {
            $cate_id = I('post.cate_id');
            if($cate_id) {
                $where['cate_id'] = $cate_id;

            }
        } else {
            $cate_id = I('post.cate_id');
            if($cate_id) {
                $where['cate_id'] = $cate_id;

            }
        }

        $count = $this->video_link_model
            ->where($where)
            ->count();
        $page = $this->page($count, C('PAGE_NUMBER'));
        $result = $this->video_link_model
            ->where($where)
            ->limit($page->firstRow, $page->listRows)
            ->select();//dump($this->video_link_model->getLastSql());
        foreach ($result as $k => $v) {
            $result[$k]['str_manage'] .= '<a class="js-ajax-btn-dialog" href="' . U('VideoLink/edit', ['id' => $v['id']]) . '">编辑</a> | <a class="js-ajax-btn-dialog js-ajax-delete" href="' . U('VideoLink/delete', ['id' => $v['id']]) . '">删除</a>';
//            $strVipFlag=($v['recommend']>0)?"<span style='color:red;'>推荐</span>":"非推荐";
            $lists .= '<tr>
               <td>' . ($k + 1) . '</td>
               <td>' . ($this->category_model->where(['id' => $v['cate_id']])->getField('name')) . '</td>
               <td>' . $v['title']. '</td>
               <td><img src="' . getUrl($v['cover']) . '"></td>
               <td>'.$v['create_time'] . '</td>
               <td>' . $result[$k]['str_manage'] . '</td>
           </tr>';

        }

        $this->assign('options', $this->category_model->getOption($cate_id));
        $this->assign('lists', $lists);
        $this->assign('formget', I(''));
        $this->assign('Page',$page->show());
        $this->display();

    }

    public function edit() {
        if(IS_POST) {
            $data = I('post.post');
            $data['content'] = htmlspecialchars_decode($data['content']);
            $images = upload_local('video');
            $data['create_time'] = date('Y-m-d H:i:s');
            $data['update_time'] = date('Y-m-d H:i:s');
            if($images) {
                $data['cover'] = $images[0];
            }//dump($data);exit;
            if(!$this->video_link_model->create($data)) {
                $this->error($this->video_link_model->getError());
            }
            if($this->video_link_model->save()) {
                $this->success('添加成功');
            } else {
                $this->error('失败，请重试');
            }
        } else {
            $id = I('get.id');
            $info = $this->video_link_model->find($id);

            $this->assign('options', $this->category_model->getOption($info['cate_id']));
            $this->assign('info', $info);
            $this->display();
        }
    }

}